package com.example.smarthome;

import java.util.*;

public class HomeManager {
    private List<SmartDevice> devices = new ArrayList<>();

    public void addDevice(SmartDevice device) {
        devices.add(device);
        System.out.println(device.deviceName + " 已添加。");
    }

    public void removeDevice(String deviceId) throws DeviceNotFoundException {
        boolean removed = devices.removeIf(d -> d.deviceId.equals(deviceId));
        if (!removed) throw new DeviceNotFoundException("设备ID " + deviceId + " 不存在！");
        System.out.println("设备 " + deviceId + " 已移除。");
    }

    public SmartDevice findDevice(String deviceId) throws DeviceNotFoundException {
        return devices.stream()
                .filter(d -> d.deviceId.equals(deviceId))
                .findFirst()
                .orElseThrow(() -> new DeviceNotFoundException("未找到设备：" + deviceId));
    }

    public void turnOnDevice(String deviceId) throws DeviceNotFoundException {
        findDevice(deviceId).turnOn();
    }

    public void turnOffDevice(String deviceId) throws DeviceNotFoundException {
        findDevice(deviceId).turnOff();
    }

    public void displayAllDevices() {
        System.out.println("\n📋 当前所有设备状态：");
        for (SmartDevice d : devices) {
            System.out.println(d.getStatus() + " 类型：" + d.getDeviceType());
        }
    }
}